package com.souku.finance.application.widget.titlebar;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.souku.finance.application.R;

/**
 * Created by Administrator on 2016/7/5.
 */
public class EaseTitleBar extends BaseTitleBar {

    private RelativeLayout rlRoot;

    private ImageView titleImage;
    private ImageView leftImage;
    private ImageView rightImage;

    private TextView rightText;
    private TextView titleText;

    private View divide;

    public EaseTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    public EaseTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EaseTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        LayoutInflater.from(context).inflate(R.layout.widget_ease_title_bar, this);

        titleText = (TextView) findViewById(R.id.title_text);
        titleImage = (ImageView) findViewById(R.id.title_image);
        rightText = (TextView) findViewById(R.id.right_text);
        leftImage = (ImageView) findViewById(R.id.left_image);
        rightImage = (ImageView) findViewById(R.id.right_image);
        rlRoot = (RelativeLayout) findViewById(R.id.rl_root);
        divide = (View) findViewById(R.id.divide);

        titleText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitleCentreView(v);
                }
            }
        });

        titleImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitleCentreView(v);
                }
            }
        });

        rightText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitleRightView(v);
                }
            }
        });

        rightImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitleRightView(v);
                }
            }
        });

        leftImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitleLeftView(v);
                }
            }
        });

        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EaseTitleBar);

            String title = ta.getString(R.styleable.EaseTitleBar_titleBarTitle);
            if (title != null) {
                titleText.setText(title);
            }

            String rightTitle = ta.getString(R.styleable.EaseTitleBar_titleBarRightTitle);
            if (rightTitle != null) {
                rightText.setText(rightTitle);
            }

            Drawable titleDrawable = ta.getDrawable(R.styleable.EaseTitleBar_titleBarTitleImage);
            if (null != titleDrawable) {
                titleImage.setImageDrawable(titleDrawable);
            }

            Drawable leftDrawable = ta.getDrawable(R.styleable.EaseTitleBar_titleBarLeftImage);
            if (null != leftDrawable) {
                leftImage.setImageDrawable(leftDrawable);
            }

            Drawable rightDrawable = ta.getDrawable(R.styleable.EaseTitleBar_titleBarRightImage);
            if (null != rightDrawable) {
                rightImage.setImageDrawable(rightDrawable);
            }

            Drawable background = ta.getDrawable(R.styleable.EaseTitleBar_titleBarBackground);
            if (null != background) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    rlRoot.setBackgroundDrawable(background);
                } else {
                    rlRoot.setBackground(background);
                }
            }

            ta.recycle();
        }
    }

    public void setTitle(String title) {
        titleText.setText(title);
    }

    public void setTitleColor(int color) {
        titleText.setTextColor(color);
    }

    public void setRightTitle(String title) {
        rightText.setText(title);
    }

    public void setRightTitleSize(float size) {
        rightText.setTextSize(size);
    }

    public void setRightTitleColor(int color) {
        rightText.setTextColor(color);
    }

    public void setRightImage(int id){
        rightImage.setBackgroundResource(id);
    }

    public void setDivideColor(int color) {
        divide.setBackgroundColor(color);
    }

    public void showDivide() {
        divide.setVisibility(VISIBLE);
    }

    public void hideDivide() {
        divide.setVisibility(INVISIBLE);
    }

    public void setBackgroundColor(int color) {
        rlRoot.setBackgroundColor(color);
    }

    @Override
    public void showTitleBar() {
        rlRoot.setVisibility(VISIBLE);
    }

    @Override
    public void hideTitleBar() {
        rlRoot.setVisibility(GONE);
    }


}
